package cn.tinkling.youdao;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

public final class JsonHelper {

	private JsonHelper() {
	}

	public final static BasicExplain getBasicExplainEntity(Gson gson,
			JsonObject json) {
		return gson.fromJson(json.get("basic"), BasicExplain.class);
	}

	public final static WebExplain[] getWebExplainEntitys(Gson gson,
			JsonObject json) {
		TypeToken<WebExplain[]> type = new TypeToken<WebExplain[]>() {
		};
		return gson.fromJson(json.get("web"), type.getType());
	}

	public final static YouDaoResult getYouDaoResultEntity(String jsonString) {
		if (jsonString == null || jsonString.trim().length() == 0)
			return null;

		JsonParser jsonParser = new JsonParser();
		JsonElement rootElement = jsonParser.parse(jsonString);
		JsonObject rootJsonObject = rootElement.getAsJsonObject();

		Gson gson = new Gson();

		YouDaoResult result = gson.fromJson(rootElement, YouDaoResult.class);
		result.setBasicExplain(getBasicExplainEntity(gson, rootJsonObject));
		result.setWebExplains(getWebExplainEntitys(gson, rootJsonObject));

		return result;
	}

}
